package com.bjdd.jdy.view.other;

import android.content.Context;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bjdd.jdy.R;
import com.bjdd.jdy.entity.ExamBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/4/13.
 */

public class QuestionLinearLayout extends LinearLayout {

    private ExamBean examBean;
    private Context mContext;
    private boolean result = false;
    private boolean hasAnswer = false;
    private List<CheckBox> checkBoxes;
    private List<RadioButton> radioButtons;
    public QuestionLinearLayout(Context context) {
        super(context);
        this.mContext = context;
        this.setOrientation(VERTICAL);
    }

    public QuestionLinearLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public QuestionLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public QuestionLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public void setExamBean(ExamBean examBean) {
        this.examBean = examBean;
        if (examBean.isMulti()){
            checkBoxes = new ArrayList<>();
        }else {
            radioButtons = new ArrayList<>();
        }
        initView();
    }

    private void initView(){
        View view2 = new View(mContext);
        addView(view2, RelativeLayout.LayoutParams.MATCH_PARENT,20);
        TextView question = new TextView(mContext);
        question.setText(examBean.getTitle());
        addView(question, RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        if (examBean.isMulti()){
            addChexkBox();
        }else {
            addRadioBtns();
        }
        View view = new View(mContext);//分割线
        view.setBackgroundColor(R.color.dividing_line);
        View view1 = new View(mContext);
        addView(view1, RelativeLayout.LayoutParams.MATCH_PARENT,20);
        addView(view, RelativeLayout.LayoutParams.MATCH_PARENT,1);
    }

    /**
     * 多选题，添加chexkbox
     */
    private void addChexkBox(){
        for (int i=0;i<examBean.getAnswers().size();i++){
            CheckBox checkBox = new CheckBox(mContext);
            examBean.getAnswers().get(i).setChexked(false);//默认没有选中
            checkBox.setText(examBean.getAnswers().get(i).getContent());
            final int j = i;
            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    examBean.getAnswers().get(j).setChexked(isChecked);
                }
            });
            if (Build.VERSION.SDK_INT <= 16){
                checkBox.setPadding(60,0,0,0);
            }else {
                checkBox.setPadding(10,0,0,0);
            }
            checkBoxes.add(checkBox);
            addView(checkBox);

        }
    }
    /**
     * 单选题，添加RadioGroup
     */
    private void addRadioBtns(){
        RadioGroup radioGroup = new RadioGroup(mContext);
        radioGroup.setOrientation(VERTICAL);
        //radioGroup.setPadding(10,0,0,0);
        for (int i=0;i<examBean.getAnswers().size();i++){
            RadioButton radioButton = new RadioButton(mContext);
            examBean.getAnswers().get(i).setChexked(false);//默认没有选中
            radioButton.setText(examBean.getAnswers().get(i).getContent());
            final int j = i;
            radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    examBean.getAnswers().get(j).setChexked(isChecked);
                }
            });
            if (Build.VERSION.SDK_INT <= 16){
                radioButton.setPadding(60,0,0,0);
            }else {
                radioButton.setPadding(10,0,0,0);
            }
            LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            lp.setMargins(20,0,0,0);
            radioButton.setLayoutParams(lp);
            radioButtons.add(radioButton);
            radioGroup.addView(radioButton);
        }
        addView(radioGroup);
    }


    public boolean isResult() {
        for (int i=0;i<examBean.getAnswers().size();i++){
            if (examBean.getAnswers().get(i).isResult() != examBean.getAnswers().get(i).isChexked()){
                return false;
            }
        }
        return true;
    }

    public boolean isHasAnswer() {
        if (examBean.isMulti()){
            for (int i=0;i<checkBoxes.size();i++){
                if (checkBoxes.get(i).isChecked()){
                    return true;
                }
            }
            return false;
        }else {
            for (int i=0;i<radioButtons.size();i++){
                if (radioButtons.get(i).isChecked()){
                    return true;
                }
            }
            return false;
        }
    }
}
